package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;

import com.jcfk.common.business.log.annotation.RequestLogHeader;

import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.system.domain.dto.OaKeyworkWeeklyActualDTO;
import com.jcfk.system.domain.po.OaKeyworkWeeklyActual;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.system.domain.query.OaKeyworkWeeklyQuery;
import com.jcfk.system.domain.dto.OaKeyworkWeeklyDTO;
import com.jcfk.system.domain.dto.OaKeyworkWeeklyPlanDTO;
import com.jcfk.system.service.OaKeyworkWeeklyService;

import java.util.Calendar;

import java.util.List;


/**
 * 周报计划事项
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-04-02
 */
@Api(tags = {"周报计划事项管理"})
@RestController
@RequestMapping("oakeyworkweekly")
public class OaKeyworkWeeklyController {
    @Autowired
    private OaKeyworkWeeklyService oaKeyworkWeeklyService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<OaKeyworkWeeklyQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询周报计划事项")
    @PostMapping("/queryByPage")
    public RetPage<OaKeyworkWeeklyDTO> queryByPage(@RequestBody ReqPageParam<OaKeyworkWeeklyQuery> requestParam) {
        DataPage<OaKeyworkWeeklyDTO> page = new DataPage<OaKeyworkWeeklyDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                OaKeyworkWeeklyDTO.class);
        requestParam.getData().setCreatedUserId(SecurityUtils.getUserId());
        page = oaKeyworkWeeklyService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<OaKeyworkWeeklyQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询周报计划事项")
    @PostMapping("/queryAllByPage")
    public RetPage<OaKeyworkWeeklyDTO> queryAllByPage(@RequestBody ReqPageParam<OaKeyworkWeeklyQuery> requestParam) {
        DataPage<OaKeyworkWeeklyDTO> page = new DataPage<OaKeyworkWeeklyDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                OaKeyworkWeeklyDTO.class);
        page = oaKeyworkWeeklyService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "OaKeyworkWeeklyQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取周报计划事项列表", notes = "获取周报计划事项列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<OaKeyworkWeeklyPlanDTO>> getDetails(@RequestBody OaKeyworkWeeklyQuery query) {

        List<OaKeyworkWeeklyPlanDTO> list = oaKeyworkWeeklyService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "OaKeyworkWeeklyQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取周报计划事项列表", notes = "获取周报计划事项列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<OaKeyworkWeeklyDTO>> getList(@RequestBody OaKeyworkWeeklyQuery query) {

        List<OaKeyworkWeeklyDTO> list = oaKeyworkWeeklyService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 周报计划事项
     * 根据id 查询详情
     */
    @ApiOperation("查询周报计划事项")
    @GetMapping("/get/{id}")
    public RetResult<OaKeyworkWeeklyDTO> get(@PathVariable("id") String id) {
        OaKeyworkWeeklyDTO item = oaKeyworkWeeklyService.get(id);
        return RetResult.ok(item);
    }


    /**
     * 周报计划事项
     * 根据id 查询详情
     */
    @ApiOperation("查询周报计划事项")
    @PostMapping("/get")
    public RetResult<?> get(@RequestBody OaKeyworkWeeklyDTO oaKeyworkWeekly) {
        List<OaKeyworkWeeklyActualDTO> item = oaKeyworkWeeklyService.lastWeekPlans(oaKeyworkWeekly);
        return RetResult.ok(item);
    }


    /**
     * 周报计划事项
     * 根据id 查询详情
     */
    @ApiOperation("查询周报计划事项")
    @PostMapping("/getWeek/{year}")
    public RetResult<?> getWeek(@PathVariable("year") int year) {

        // 计算一年的周数
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        int weeksInYear = calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);

        // 输出周数
        System.out.println(year + "年共有" + weeksInYear + "周。");
        return RetResult.ok(weeksInYear);
    }

    /**
     * 周报计划事项
     * 根据id 查询详情
     */
    @ApiOperation("新增未完成计划")
    @GetMapping("/lastWeekPlans")
    public RetResult<OaKeyworkWeeklyDTO> lastWeekPlans(@PathVariable("id") String id) {
        OaKeyworkWeeklyDTO item = oaKeyworkWeeklyService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理周报计划事项
     */
    @ApiOperation("保存周报计划事项")
    @PostMapping("/save")
    @RequestLogHeader(modular = "周报计划事项管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid OaKeyworkWeeklyDTO oaKeyworkWeekly) {
        // 数据校验
        RetResult ret = oaKeyworkWeeklyService.isValid(oaKeyworkWeekly);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(oaKeyworkWeekly.getId())) {
            // 新增
            oaKeyworkWeeklyService.insert(oaKeyworkWeekly);
        } else {
            // 更新
            oaKeyworkWeeklyService.update(oaKeyworkWeekly);
        }
        return RetResult.ok();
    }


    /**
     * 批量删除周报计划事项
     */
    @ApiOperation("批量删除周报计划事项")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "周报计划事项管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody OaKeyworkWeeklyDTO oaKeyworkWeekly) {
        return RetResult.ok(oaKeyworkWeeklyService.logicDeleteList(oaKeyworkWeekly));
    }

}
